<?php
/*
 * @FilePath: \crm\application\crm\controller\Index.php
 */

namespace app\crm\controller;

use app\crm\model\auth\Admin;
use think\facade\Session;
use app\crm\model\auth\AuthNode;
use app\crm\model\auth\Role;
use app\crm\model\auth\YdCrmAdmin;
use Exception;

class Index extends BaseController
{
    public function index()
    {
        $this->jurisdiction();
        return $this->fetch();
    }

    public function welcome()
    {
        return $this->fetch();
    }

    /**
     * 根据用户权限重新生成节点
     * @Date: 2022-12-03 18:38:26
     */
    public function jurisdiction()
    {
        $admin = Session::get('admin');
        $json = file_get_contents('static/crm/api/init.json');
        $data = json_decode($json, true);
        $NodeList = explode(',', Role::where('id', $admin['role_id'])->find()['node_id']);
        $admin['id'] == 1 && $NodeList = range(1, 168);

        $list = AuthNode::where('parent_id', 0)->field('id,title')->all();
        foreach ($list as $val) {
            if (array_search($val['id'], $NodeList) !== false) {
                $list2 = AuthNode::where('parent_id', $val['id'])->all();
                $child2 = [];

                foreach ($list2 as $val2) {
                    if (array_search($val2['id'], $NodeList) !== false) {
                        $list3 = AuthNode::where('parent_id', $val2['id'])->all();
                        $child3 = [];
                        foreach ($list3 as $val3) {
                            if (array_search($val3['id'], $NodeList) !== false) {
                                $list4 = AuthNode::where('parent_id', $val3['id'])->all();
                                $child4 = [];
                                foreach ($list4 as $val4) {
                                    if (array_search($val4['id'], $NodeList) !== false) {
                                        $val4['title'] == '列表' && $index2 = $val4['node'];
                                        $child4[] = ['title' => $val4['title'], "icon" => 'fa fa-meetup'];
                                    }
                                }

                                $val3['title'] == '列表' && $index = $val3['node'];
                                $child3[] = ['title' => $val3['title'], "icon" => 'fa fa-meetup', 'child' => $child4];
                                if ($val3['is_last'] == 1) {
                                    if (!empty($child3[array_key_last($child3)]['child'])) {
                                        if ($child3[array_key_last($child3)]['child'][0]['title'] != '列表') {
                                            unset($child3[array_key_last($child3)]);
                                        } else {
                                            $child3[array_key_last($child3)] = ['title' => $val3['title'], "icon" => 'fa fa-meetup', 'href' => $index2];
                                        }
                                    } else {
                                        unset($child3[array_key_last($child3)]);
                                    }
                                }
                            }
                        }

                        $child2[] = ['title' => $val2['title'], "icon" => 'fa fa-meetup', 'child' => $child3];
                        if ($val2['is_last'] == 1) {
                            if (!empty($child2[array_key_last($child2)]['child'])) {
                                if ($child2[array_key_last($child2)]['child'][0]['title'] != '列表') {
                                    unset($child2[array_key_last($child2)]);
                                } else {
                                    $child2[array_key_last($child2)] = ['title' => $val2['title'], "icon" => 'fa fa-meetup', 'href' => $index];
                                }
                            } else {
                                unset($child2[array_key_last($child2)]);
                            }
                        }
                    }
                }
                $data['menuInfo'][] = ['title' => $val['title'], 'child' => array_values($child2)];
            }
        }
        file_put_contents('static/crm/api/init2.json', json_encode($data));
    }

    public function editAdmin()
    {
        $id = session('admin.id');
        $row = Admin::get($id);
        empty($row) && $this->error('账户不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [
                'account|登录账户' => 'require',
                'phone|手机号' => 'mobile'
            ];
            $result = $this->validate($post, $rule);
            $result !== true && $this->error($result);
            try {
                if ($row['account'] != $post['account']) {
                    if (!empty(Admin::where('account', $post['account'])->find())) {
                        throw new Exception('该账号已存在', 0);
                    }
                    $save = $row->save($post);
                } else {
                    $save = $row->save($post);
                }
            } catch (\Exception $e) {
                $e->getCode() == 0 && $this->error($e->getMessage());
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        return $this->fetch('', [
            'row' => $row
        ]);
    }

    public function editPassword()
    {
        $id = session('admin.id');
        $row = Admin::get($id);
        if (!$row) {
            $this->error('用户信息不存在');
        }
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [
                'password|新密码' => 'require|confirm'
            ];
            $result = $this->validate($post, $rule);
            $result !== true && $this->error($result);
            $post['password'] = md5($post['password']);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        return $this->fetch('', [
            'row' => $row
        ]);
    }
}
